package net.lab1024.smartadmin.module.business.front.service;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import net.lab1024.smartadmin.common.domain.PageResultDTO;
import net.lab1024.smartadmin.common.domain.ResponseDTO;
import net.lab1024.smartadmin.module.business.front.dao.FrontDao;
import net.lab1024.smartadmin.module.business.front.domain.dto.FrontAddDTO;
import net.lab1024.smartadmin.module.business.front.domain.dto.FrontQueryDTO;
import net.lab1024.smartadmin.module.business.front.domain.dto.FrontUpdateDTO;
import net.lab1024.smartadmin.module.business.front.domain.entity.FrontEntity;
import net.lab1024.smartadmin.module.business.front.domain.vo.FrontExcelVO;
import net.lab1024.smartadmin.module.business.front.domain.vo.FrontVO;
import net.lab1024.smartadmin.util.SmartBeanUtil;
import net.lab1024.smartadmin.util.SmartPageUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * [ 牡丹花 ]
 *
 */
@Service
public class FrontService {

    @Autowired
    private FrontDao frontDao;

    /**
     * 根据id查询
     */
    public FrontEntity getById(Long id){
        return  frontDao.selectById(id);
    }

    /**
     * 分页查询
     * @author 卓大
     * @date 2020-04-06 18:17:56
     */
    public ResponseDTO<PageResultDTO<FrontVO>> queryByPage(FrontQueryDTO queryDTO) {
        Page page = SmartPageUtil.convert2QueryPage(queryDTO);
        IPage<FrontVO> voList = frontDao.queryByPage(page, queryDTO);
        PageResultDTO<FrontVO> pageResultDTO = SmartPageUtil.convert2PageResult(voList);
        return ResponseDTO.succData(pageResultDTO);
    }

    /**
     * 添加
     * @author 卓大
     * @date 2020-04-06 18:17:56
     */
    public ResponseDTO<String> add(FrontAddDTO addDTO) {
        FrontEntity entity = SmartBeanUtil.copy(addDTO, FrontEntity.class);
        frontDao.insert(entity);
        return ResponseDTO.succ();
    }

    /**
     * 编辑
     * @author 卓大
     * @date 2020-04-06 18:17:56
     */
    @Transactional(rollbackFor = Exception.class)
    public ResponseDTO<String> update(FrontUpdateDTO updateDTO) {
        FrontEntity entity = SmartBeanUtil.copy(updateDTO, FrontEntity.class);
        frontDao.updateById(entity);
        return ResponseDTO.succ();
    }

    /**
     * 删除
     * @author 卓大
     * @date 2020-04-06 18:17:56
     */
    @Transactional(rollbackFor = Exception.class)
    public ResponseDTO<String> deleteByIds(List<Long> idList) {
        frontDao.deleteByIdList(idList);
        return ResponseDTO.succ();
    }

    /**
     * 查询全部导出对象
     * @author 卓大
     * @date 2020-04-06 18:17:56
     */
    public List<FrontExcelVO> queryAllExportData(FrontQueryDTO queryDTO) {
        return frontDao.queryAllExportData( queryDTO);
    }

    /**
     * 批量查询导出对象
     * @author 卓大
     * @date 2020-04-06 18:17:56
     */
    public List<FrontExcelVO> queryBatchExportData(List<Long> idList) {
        return frontDao.queryBatchExportData(idList);
    }
}
